package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.maindata.domain.Line;
import com.qijian.maindata.domain.LineBom;
import com.qijian.maindata.domain.query.LineBomQuery;
import com.qijian.maindata.domain.query.LineProcessQuery;
import com.qijian.maindata.domain.query.LineQuery;
import com.qijian.maindata.domain.vo.LineVo;
import com.qijian.maindata.service.ILineProcessService;
import com.qijian.maindata.service.ILineService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 工艺线路Controller
 *
 * @author qijian
 * @date 2021-09-03
 */
@Api(tags = "工艺管理：工艺线路")
@RestController
@RequiredArgsConstructor
@RequestMapping("/maindata/line")
public class LineController extends BaseController {

    private final ILineService lineService;
    private final ILineProcessService lineProcessService;

    /**
     * 查询工艺线路列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:line:list')")
    @GetMapping("/list")
    public TableDataInfo list(LineQuery query, Page<LineVo> page) {
        return getDataTable(lineService.pageVo(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 查询工艺线路列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:line:query')")
    @GetMapping("/all")
    public TableDataInfo all(LineQuery query) {
        return getDataTable(lineService.listVo(WhereEntityTool.invoke(query)));
    }

    /**
     * 导出工艺线路列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:line:export')")
    @Log(title = "工艺线路", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LineQuery query) {
        return new ExcelUtil<>(Line.class).exportExcel(lineService.list(WhereEntityTool.invoke(query)), "工艺线路数据");
    }

    /**
     * 获取工艺线路详细信息
     */
    @PreAuthorize("@ss.hasPermi('maindata:line:query')")
    @GetMapping(value = "/{lineId}")
    public AjaxResult getInfo(@PathVariable("lineId") Long lineId) {
        return AjaxResult.success(lineService.getById(lineId));
    }

    @PreAuthorize("@ss.hasPermi('maindata:line:query')")
    @GetMapping(value = "/processList")
    public AjaxResult processList(LineProcessQuery query) {
        return AjaxResult.success(lineProcessService.list(WhereEntityTool.invoke(query)));
    }

    /**
     * 新增工艺线路
     */
    @PreAuthorize("@ss.hasPermi('maindata:line:add')")
    @Log(title = "工艺线路", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Line line) {
        return toAjax(lineService.save(line));
    }

    /**
     * 新增工艺线路
     */
    @PreAuthorize("@ss.hasPermi('maindata:line:add')")
    @Log(title = "工艺线路", businessType = BusinessType.INSERT)
    @PostMapping("/bom")
    public AjaxResult add(@RequestBody LineBom lineBom) {
        LineBomQuery query = new LineBomQuery();
        query.setLineId(lineBom.getLineId());
        query.setItemId(lineBom.getItemId());
        query.setBomId(lineBom.getBomId());
        if (lineService.bomCount(WhereEntityTool.invoke(query)) == 0) {
            lineService.saveDetail(Collections.singletonList(lineBom));
        }
        return AjaxResult.success();
    }

    /**
     * 修改工艺线路
     */
    @PreAuthorize("@ss.hasPermi('maindata:line:edit')")
    @Log(title = "工艺线路", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Line line) {
        return toAjax(lineService.updateById(line));
    }

    /**
     * 删除工艺线路
     */
    @PreAuthorize("@ss.hasPermi('maindata:line:remove')")
    @Log(title = "工艺线路", businessType = BusinessType.DELETE)
    @DeleteMapping("/{lineIds}")
    public AjaxResult remove(@PathVariable List<Long> lineIds) {
        return toAjax(lineService.removeByIds(lineIds));
    }
}
